package com.tinycloud.miracll.product.service.impl;

import com.tinycloud.common.core.utils.DateUtils;
import com.tinycloud.common.core.utils.StringUtils;
import com.tinycloud.common.datasource.annotation.Master;
import com.tinycloud.common.datasource.annotation.Slave1;
import com.tinycloud.miracll.product.domain.DeviationDataDetail;
import com.tinycloud.miracll.product.domain.DeviationDataMain;
import com.tinycloud.miracll.product.mapper.DeviationDataMainMapper;
import com.tinycloud.miracll.product.service.IDeviationDataMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 偏差数据主表Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-30
 */
@Service
@Master

public class DeviationDataMainServiceImpl implements IDeviationDataMainService
{
    @Autowired
    private DeviationDataMainMapper deviationDataMainMapper;

    /**
     * 查询偏差数据主表
     * 
     * @param orderNum 偏差数据主表ID
     * @return 偏差数据主表
     */
    @Override
    public DeviationDataMain selectDeviationDataMainById(String orderNum)
    {
        return deviationDataMainMapper.selectDeviationDataMainById(orderNum);
    }

    /**
     * 查询偏差数据主表列表
     * 
     * @param deviationDataMain 偏差数据主表
     * @return 偏差数据主表
     */
    @Override
    public List<DeviationDataMain> selectDeviationDataMainList(DeviationDataMain deviationDataMain)
    {
        return deviationDataMainMapper.selectDeviationDataMainList(deviationDataMain);
    }

    /**
     * 新增偏差数据主表
     * 
     * @param deviationDataMain 偏差数据主表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDeviationDataMain(DeviationDataMain deviationDataMain)
    {
        deviationDataMain.setCreateTime(DateUtils.getNowDate());
        int rows = deviationDataMainMapper.insertDeviationDataMain(deviationDataMain);
        insertDeviationDataDetail(deviationDataMain);
        return rows;
    }

    /**
     * 修改偏差数据主表
     * 
     * @param deviationDataMain 偏差数据主表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDeviationDataMain(DeviationDataMain deviationDataMain)
    {
        deviationDataMainMapper.deleteDeviationDataDetailByProdInfoID(deviationDataMain.getOrderNum());
        insertDeviationDataDetail(deviationDataMain);
        return deviationDataMainMapper.updateDeviationDataMain(deviationDataMain);
    }

    /**
     * 批量删除偏差数据主表
     * 
     * @param orderNums 需要删除的偏差数据主表ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDeviationDataMainByIds(String[] orderNums)
    {
        deviationDataMainMapper.deleteDeviationDataDetailByProdInfoIDs(orderNums);
        return deviationDataMainMapper.deleteDeviationDataMainByIds(orderNums);
    }

    /**
     * 删除偏差数据主表信息
     * 
     * @param orderNum 偏差数据主表ID
     * @return 结果
     */
    @Override
    public int deleteDeviationDataMainById(String orderNum)
    {
        deviationDataMainMapper.deleteDeviationDataDetailByProdInfoID(orderNum);
        return deviationDataMainMapper.deleteDeviationDataMainById(orderNum);
    }

    /**
     * 新增偏差数据明细表信息
     * 
     * @param deviationDataMain 偏差数据主表对象
     */
    public void insertDeviationDataDetail(DeviationDataMain deviationDataMain)
    {
        List<DeviationDataDetail> deviationDataDetailList = deviationDataMain.getDeviationDataDetailList();
        String orderNum = deviationDataMain.getOrderNum();
        if (StringUtils.isNotNull(deviationDataDetailList))
        {
            List<DeviationDataDetail> list = new ArrayList<DeviationDataDetail>();
            for (DeviationDataDetail deviationDataDetail : deviationDataDetailList)
            {
                deviationDataDetail.setProdInfoID(orderNum);
                list.add(deviationDataDetail);
            }
            if (list.size() > 0)
            {
                deviationDataMainMapper.batchDeviationDataDetail(list);
            }
        }
    }
}
